package point24.util;

import java.util.*;


public class RandomSearcherImp implements Searcher {

    private static final int MAX_COUNTS = 500;


    public static void main(String[] args) {

        for (String result : new RandomSearcherImp().search(new Integer[]{1, 2, 4, 5})) {
            System.out.println(result);
        }
    }

    @Override
    public List<String> search(Integer[] arr) {
        Set<String> resultList = new TreeSet<>();
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                for (int k = 0; k < 4; k++) {
                    String x = OPERATORS[i];
                    String y = OPERATORS[j];
                    String z = OPERATORS[k];
                    List<String> list = new Vector<>();
                    for (int n : arr) list.add(n + "");
                    list.add(x);
                    list.add(y);
                    list.add(z);
                    //  System.err.println(list);
                    int time = 0;
                    while (++time <= MAX_COUNTS) {
                        Collections.shuffle(list);
                        if (Math.abs(ReversePolishNotation.eval(list) - TARGET) < EPS) {
                            resultList.add(BinaryTree.convertRpnToNormalExpression(list));
                        }
                    }
                }
        if (resultList.isEmpty()) resultList.add("No solution");

        return new Vector<>(resultList);
    }
}